﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace EM.definition.finance.Bank
{
    public partial class branch : System.Web.UI.Page
    {
        int pageId = 29;
        DbHelper srv = new DbHelper();
        FillCombo fc = new FillCombo();
        private int bankId;

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(pageId, Authentication.RedirectionURLTypes.LoginPage);


            if (Request.QueryString["BANKID"] != null)
                int.TryParse(Request.QueryString["BANKID"].ToString(), out bankId);

            if (!Page.IsPostBack)
            {
                this.GetData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.GetData();
        }

        private void GetData()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(@"SELECT BANKBRANCHID, BANKID, BRANCHNAME, ADDRESS, PHONE, FAX, STATE, CITY
                            FROM TBLBANKBRANCH WHERE BANKID = @BANKID 
                            ORDER BY BRANCHNAME");

            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@BANKID", this.bankId);


            cmd.CommandText = builder.ToString();
            DataTable dt = srv.GetDataTable(cmd, "");
            rptBranch.DataSource = dt;
            rptBranch.DataBind();
        }

        protected void rptBranch_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                TextBox txtState = e.Item.FindControl("txtState") as TextBox;
                TextBox txtBranchName = e.Item.FindControl("txtBranchName") as TextBox;
                TextBox txtAddress = e.Item.FindControl("txtAddress") as TextBox;
                TextBox txtTel = e.Item.FindControl("txtTel") as TextBox;
                TextBox txtFax = e.Item.FindControl("txtFax") as TextBox;
                TextBox txtCity = e.Item.FindControl("txtCity") as TextBox;

                DataRowView row = e.Item.DataItem as DataRowView;

                ValidationSummary vsUpdate = e.Item.FindControl("vsUpdate") as ValidationSummary;
                LinkButton btnUpdate = e.Item.FindControl("btnUpdate") as LinkButton;
                RequiredFieldValidator rfvBranchName = e.Item.FindControl("rfvBranchName") as RequiredFieldValidator;

                vsUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                btnUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                rfvBranchName.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;

                txtState.Text = row["STATE"].ToString();
                txtBranchName.Text = row["BRANCHNAME"].ToString();
                txtAddress.Text = row["ADDRESS"].ToString();
                txtTel.Text = row["PHONE"].ToString();
                txtFax.Text = row["FAX"].ToString();
                txtCity.Text = row["CITY"].ToString();
            }
        }

        protected void rptBranch_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            TextBox txtState = e.Item.FindControl("txtState") as TextBox;
            TextBox txtBranchName = e.Item.FindControl("txtBranchName") as TextBox;
            TextBox txtAddress = e.Item.FindControl("txtAddress") as TextBox;
            TextBox txtTel = e.Item.FindControl("txtTel") as TextBox;
            TextBox txtFax = e.Item.FindControl("txtFax") as TextBox;
            TextBox txtCity = e.Item.FindControl("txtCity") as TextBox;
            if (e.CommandName == "Insert")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"INSERT INTO TBLBANKBRANCH
                                    (BANKID,BRANCHNAME,ADDRESS,PHONE,FAX,STATE,CITY)
                                    SELECT @BANKID,@BRANCHNAME,@ADDRESS,@PHONE,@FAX,@STATE,@CITY";
                cmd.Parameters.AddWithValue("@BANKID", this.bankId);
                cmd.Parameters.AddWithValue("@BRANCHNAME", txtBranchName.Text);
                cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text);
                cmd.Parameters.AddWithValue("@PHONE", txtTel.Text);
                cmd.Parameters.AddWithValue("@FAX", txtFax.Text);
                cmd.Parameters.AddWithValue("@STATE", txtState.Text);
                cmd.Parameters.AddWithValue("@CITY", txtCity.Text);
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Update")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"UPDATE TBLBANKBRANCH SET
                                    BANKID = @BANKID,BRANCHNAME = @BRANCHNAME,ADDRESS = @ADDRESS,
                                    PHONE = @PHONE,FAX = @FAX,STATE = @STATE,CITY = @CITY
                                    WHERE BANKBRANCHID = @BANKBRANCHID";
                cmd.Parameters.AddWithValue("@BANKID", this.bankId);
                cmd.Parameters.AddWithValue("@BRANCHNAME", txtBranchName.Text);
                cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text);
                cmd.Parameters.AddWithValue("@PHONE", txtTel.Text);
                cmd.Parameters.AddWithValue("@FAX", txtFax.Text);
                cmd.Parameters.AddWithValue("@STATE", txtState.Text);
                cmd.Parameters.AddWithValue("@CITY", txtCity.Text);
                cmd.Parameters.AddWithValue("@BANKBRANCHID", e.CommandArgument);
                
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Delete")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"DELETE TBLBANKBRANCH WHERE BANKBRANCHID = @BANKBRANCHID";
                cmd.Parameters.AddWithValue("@BANKBRANCHID", e.CommandArgument);
                srv.ExecuteNonQuery(cmd);
            }

            this.GetData();
        }
    }
}